package com.hama.iotrust.common.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

/**
 * The Class RequestUtil.
 * 
 * @author 金建强(ptma@163.com)
 * @version 1.0.0
 * @since 2013-4-7 17:16:04
 */
public class RequestUtil {

    public static boolean isMultipart(HttpServletRequest request) {
        return ((request.getContentType() != null) && (request.getContentType().toLowerCase().startsWith("multipart")));
    }

    /**
     * 获取COOKIE
     * 
     * @param name
     */
    public static Cookie getCookie(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null) return null;
        for (int i = 0; i < cookies.length; i++) {
            if (name.equals(cookies[i].getName())) {
                return cookies[i];
            }
        }
        return null;
    }

    /**
     * 设置COOKIE
     * 
     * @param name
     * @param value
     * @param maxAge
     */
    public static void setCookie(HttpServletRequest request, HttpServletResponse response, String name, String value,
                                 int maxAge) {
        Cookie cookie = new Cookie(name, value);
        cookie.setMaxAge(maxAge);
        cookie.setPath("/");
        response.addCookie(cookie);
    }

    /**
     * 从URL地址中解析出URL前缀，例如 http://www.163.com:8081/index.jsp -> http://www.163.com:8081
     * 
     * @param req
     * @return
     */
    public static String getUrlPrefix(HttpServletRequest request) {
        StringBuffer url = new StringBuffer(request.getScheme());
        url.append("://");
        url.append(request.getServerName());
        int port = request.getServerPort();
        if (port != 80) {
            url.append(":");
            url.append(port);
        }
        return url.toString();
    }

    /**
     * 获取访问的URL全路径
     * 
     * @param req
     * @return
     */
    public static String getRequestURL(HttpServletRequest request) {
        StringBuffer url = new StringBuffer(request.getRequestURI());
        String param = request.getQueryString();
        if (param != null) {
            url.append('?');
            url.append(param);
        }
        String path = url.toString();
        return path.substring(request.getContextPath().length());
    }

    /**
     * 判断浏览器是否与Mozilla兼容
     * 
     * @param req
     * @return
     */
    public static boolean isMozillaCompatible(HttpServletRequest request) {
        String user_agent = request.getHeader("user-agent");
        return user_agent == null || user_agent.indexOf("Mozilla") != -1;
    }

    /**
     * 是否为JSON请求
     * 
     * @param request
     * @return
     */
    public static boolean isJsonRequest(HttpServletRequest request) {
        String accept_header = request.getHeader("Accept");
        return accept_header != null && accept_header.indexOf("application/json") != -1;
    }

    /**
     * 获取浏览器提交的整形参数
     * 
     * @param param
     * @param defaultValue
     * @return
     */
    public static int getParam(HttpServletRequest request, String param, int defaultValue) {
        try {
            String value = request.getParameter(param);
            int idx = value.indexOf('#');
            if (idx != -1) value = value.substring(0, idx);
            return Integer.parseInt(value);
        } catch (Exception e) {
        }
        return defaultValue;
    }

    /**
     * 获取浏览器提交的字符串参数
     * 
     * @param param
     * @param defaultValue
     * @return
     */
    public static String getParam(HttpServletRequest request, String param, String defaultValue) {
        String value = request.getParameter(param);
        return (StringUtils.isEmpty(value)) ? defaultValue : value;
    }

}
